package com.noreply.springlearning;

import com.noreply.springlearning.aspect.TestBean;
import com.noreply.springlearning.customparser.User;
import com.noreply.springlearning.lookup.GetBeanTest;
import com.noreply.springlearning.service.MessageService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zqz
 * @datetime 2021/4/16 15:45
 */
public class App {

    public static void main(String[] args) {
        // 用我们的配置文件来启动一个 ApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");

        System.out.println("context 启动成功");

        // 从 context 中取出我们的 Bean，而不是用 new MessageServiceImpl() 这种方式
        MessageService messageService = context.getBean(MessageService.class);
        // 这句将输出: hello world
        System.out.println(messageService.getMessage());

        System.out.println(context.getId());
        System.out.println(context.getApplicationName());
        System.out.println(context.getParent());
        System.out.println(context.getStartupDate());
        System.out.println(context.getDisplayName());

        // spring默认注册的bean
        Map<String, Object> environment = (Map<String, Object>)context.getBean("systemEnvironment");
        System.out.println(environment.toString());

        GetBeanTest gt = context.getBean(GetBeanTest.class);
        gt.say();


        User bean = context.getBean(User.class);
        System.out.println(bean.toString());

        TestBean testBean = context.getBean(TestBean.class);
        testBean.test();
    }


}
